#include <my_header.h>
#if 0

A 从write.pipe中输入数据 从read.pipe中读取数据
需要注意顺序 A先写 后读 B先读 后写 不然会发生死锁问题
#endif
/* Usage: ./4_UserA_duplex */
int main(int argc, char *argv[]){                                  
    // 写入write管道数据 UserB从管道读取数据
    int fd_write = open("write.pipe", O_WRONLY);
    ERROR_CHECK(fd_write, -1, "open write");
    // 读取UserB写入的数据
    int fd_read = open("read.pipe", O_RDONLY);
    ERROR_CHECK(fd_read, -1, "open read");

    while(1)
    {

        char buf[60];
        // 读取标准输入：通过write.pipe发送给UserB
        bzero(buf, sizeof(buf));    // 清零
                                    // 将从stdin输入的数据写到buf
        int read_stdin = read(STDIN_FILENO, buf, sizeof(buf));
        if(read_stdin == 0)
        {
            // 用户ctrl+d;终止标准输入
            break;
        }
        // 从buf获取的数据写入到管道write.pipe
        write(fd_write, buf, sizeof(buf));
        
        // 清空buf缓冲区 为了读取UserB发来的数据
        bzero(buf, sizeof(buf));
        // 读取read.pipe:打印到标准输出
        int read_num = read(fd_read, buf, sizeof(buf));
        if(read_num == 0)
        {
            printf("对方断开连接\n");
            break;
        }
        printf("UserB: %s\n", buf);
    }

    close(fd_write);
    close(fd_read);
    return 0;
}


